<?php

namespace HuoShu;

class PtbBalance{
    public function getBalance($agent_id){
        $where=array();
        $where['agent_id']=$agent_id;
        $result= M('ptb_agent')->where($where)->getField("remain");
        $r=(float)$result;
        return $r;
    }
    
    public function Dec($agent_id,$amount){
        $ae_r=M('ptb_agent')->where(array("agent_id"=>$agent_id))->find();
        $balance=$ae_r['remain']-$amount;
        M('ptb_agent')->where(array("agent_id"=>$agent_id))->save(array("remain"=>$balance));
    }   
    
    public function Inc($agent_id,$amount){
        $model=M('ptb_agent');
        $ae_where=array("agent_id"=>$agent_id);
        $pre=$model->where($ae_where)->find();        
        if($pre){
            $new_data=array();
            $new_data['remain']=$pre['remain']+$amount;
            $new_data['total']=$pre['total']+$amount;
            $new_data['sum_money']=$pre['sum_money']+$amount;
            $new_data['update_time']=time();
            
            $model->where($ae_where)->save($new_data);
        }else{            
            $new_data=array();
            $new_data['agent_id']=$agent_id;
            $new_data['remain']=$amount;
            $new_data['total']=$amount;
            $new_data['sum_money']=$amount;
            $new_data['create_time']=time();
            $new_data['update_time']=time();

            $model->add($new_data);
        }
        
    }
}

